package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.lang.model.element.VariableElement;
import java.util.Map;

/**
 * @autahor: csl
 * @date: 2019/7/23
 * @description: 预约控制器
 * @version:1.0
 */
@RestController
@RequestMapping("/mobile/order")
public class OrderController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService service;
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String,String> map){
             String code = jedisPool.getResource().get(map.get("telephone") + RedisConst.SENDTYPE_ORDER);
             if (code==null ||!code.equals( map.get("validateCode"))){
                 return new Result(false, MessageConst.VALIDATECODE_ERROR);
             }
             Result result=null;
        try {
                map.put("orderType", Order.ORDERTYPE_WEIXIN);
                result= service.submit(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        if (result.isFlag()){
            map.put("orderType",Order.ORDERTYPE_WEIXIN);
            String orderDate = (String) map.get("orderDate");
             try {
                 // 调用三方，发送预约成功消息，通知类短信必须资质审核
                 //SMSUtils.sendShortMessage(telephone,"0000");
                     }catch (Exception e){
                         e.printStackTrace();
                     }
        }
        return result;
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
         try {
                 Map<String,Object>map= service.findById(id);
                 return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
                 }catch (Exception e){
                     e.printStackTrace();
                 return new Result(false,MessageConst.QUERY_ORDER_FAIL);

                 }
    }
}
